import React, {Component} from 'react';
import {View, BackHandler} from 'react-native';
import {toast} from './GlobalToast';
import {Actions} from 'react-native-router-flux';

export default class DoubleExitApp extends Component {
	
	constructor(...args) {
		super(...args);
		this.state = {
			lastBackPressed: -1,
		}
		this.onBackAndroid = this.onBackAndroid.bind(this);
	}
	
  	render() {
        return <View />;
	}
  	
  	componentWillMount() {
    	BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);  
	}
	
	componentWillUnMount() {
		BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);  
	}
  	
  	onBackAndroid() {
  		let scenes = this.props.scenes;
  		let isQ = false;
  		if(scenes) {
  			for(let s of scenes) {
  				let i = Actions.currentScene.indexOf(s);
  				if(i == 0 || i == 1) {
  					isQ = true;
  					break;
  				}
  			}
  		}
  		if(isQ) {
  			let lastBackPressed = this.state.lastBackPressed;
		    let now = new Date().getTime();
		    if(now - lastBackPressed < 2500) {
		    	BackHandler.exitApp();
		        return false;
		    }
		    this.setState({lastBackPressed: now});
		    toast().show('再点击一次退出应用', 2000);
  		} else {
  			Actions.pop();
  		}
  		return true;
	}
  	
}